package com.yonyou.iuap.jxzy.servlet;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.jxzy.utils.HttpRequestUtil;

/**
 * 读卡监控控制器
 * @author WXF
 *
 */
public class AutoReadThread implements Runnable{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private DeviceInfo deviceinfo;
	
	private BaedDeviceService service;
	
	public static final String DEVURL = "http://164.70.12.29:8080/unattended_hardware/hardwareOperation/readCardJX";
	
	public AutoReadThread(DeviceInfo DeviceInfo){
		try {
			this.deviceinfo = DeviceInfo;
			Class<?> cls = Class.forName(DeviceInfo.getExtendClass());
			service = (BaedDeviceService) ApplicationContextHolder.getBeanByClass(cls);
		} catch (Exception e) {
			logger.error("初始化扩展业务处理类失败，设备ID："+DeviceInfo.getDeviceCode()+"; class:"+deviceinfo.getExtendClass());
		}
	}
	
	@Override
	public void run() {
		System.out.println("执行读卡操作，设备id："+deviceinfo.getDeviceCode());
		try {
			//调用硬件读卡
			Map<String,String> map = new HashMap<String,String>();
			map.put("deviceCode", deviceinfo.getDeviceCode());
			JSONObject object =new JSONObject();
			object.put("deviceCode", deviceinfo.getDeviceCode());
			String cardid = HttpRequestUtil.sendPost(DEVURL, object.toJSONString());
			System.out.println("网口读卡器卡id："+cardid);
			if(cardid != null && !cardid.equals("0") && cardid.length() > 1){
				deviceinfo.setUserData(cardid);
				service.processor(deviceinfo);
				deviceinfo.setUserData(null);//清空上下文信息
			}
		} catch (Throwable e) {
			e.printStackTrace();
			logger.error("执行读卡异常！异常信息："+e.getMessage());
		}
	}

}
